/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.lcfgly.View;

import com.lcfgly.Dao.CountyImpl;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author lcfgly
 */
@Controller
public class Address {

    @Autowired
    CountyImpl countyImpl;

    //开度应用key
    private final String ak = "GUk14zGfe4WxVaKLqGWsC8Mz";

    @RequestMapping("/getaddress/{id}")
    @ResponseBody
    public Map GetAddress(@PathVariable("id") int id) {
        Map result = new HashMap();
        result.put("success", false);
        try {
            result.put("row", countyImpl.findInfo(id));
            result.put("success", true);
        } catch (SQLException e) {
            System.out.println(e);
        }
        return result;
    }

    @RequestMapping("/geocodeing")
    @ResponseBody
    public String GeoCodeing(@RequestParam("ads") String address, @RequestParam("cityid") int cityid) {
        if(address.isEmpty()){
            return "{status:1}";
        }
        address=countyImpl.GetAddress(cityid)+address;
        String url = "http://api.map.baidu.com/geocoder/v2/?ak=" + ak + "&output=json&address="+address ;
        StringBuilder result=new StringBuilder();
        try {
            URL u= new URL(url);
            HttpURLConnection req = (HttpURLConnection) u.openConnection();
            req.connect();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    req.getInputStream()));
            String lines;
            while ((lines = reader.readLine()) != null) {
                result.append(lines);
            }
            reader.close();
        } catch (IOException ex) {
            System.out.println(ex);
        }
        return result.toString();
    }
    
}
